/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.app.facade.Facade;
import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.AlbumCategory;
import com.startup.musicstore.services.InventoryService;
import com.startup.musicstore.services.crud.AlbumCategoryCrudService;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author aljay19
 */
@Service("inventoryService")
@Transactional
public class InventoryServiceImpl implements InventoryService {
    
    private Facade data = new Facade();
    
    private AlbumCategoryCrudService albumCategoryCrudService;

    @Override
    public String adjustInventory(int orderQuantity, Long albumID, Long albumCategoryID) {
        
        albumCategoryCrudService = data.getAlbumCategoryCrudService();
        AlbumCategory albumCategory = albumCategoryCrudService.findById(albumCategoryID);
        List<Album> albums = albumCategory.getAlbums();
        String updated = "notUpdated";

        for (Album a : albums) {
            if (albumID.equals(a.getId())) {
                a.setInventory(a.getInventory() - orderQuantity);
                a.setSales(a.getSales() + orderQuantity);
                updated = "updated";
            }
        }

        albumCategory.setAlbums(albums);
        albumCategoryCrudService.merge(albumCategory);

        return updated;
    }
}
